package drone.basis.base ;

import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpSession ;

import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.RequestMapping ;

import drone.basis.util.MobileUtil ;
import drone.basis.util.StringUtil ;

public class BaseController
{

	private static final Logger logger = LoggerFactory.getLogger ( BaseController.class ) ;

	public String name ;

	@Autowired
	protected HttpServletRequest request ;

	@Autowired
	protected HttpServletResponse response ;

	@Autowired
	protected HttpSession session ;

	public BaseController (
			String name
	)
	{
		this.name = name ;
	}

	@RequestMapping ( "{action}" )
	public String action (
			@PathVariable String action
	)
	{
		logger.info ( "Request Path : " + action + " Return Path : " + peer ( ) + "/" + name + "/" + action ) ;
		if ( StringUtil.isEmpty ( name ) )
		{
			return peer ( ) + "/" + action ;
		}
		return peer ( ) + "/" + name + "/" + action ;
	}

	protected String peer ( )
	{
		if ( request.getServerName ( ).indexOf ( "localhost" ) >= 0 )
		{
			return "web" ;
		}
		if ( request.getServerName ( ).indexOf ( "127.0.0.1" ) >= 0 )
		{
			return "wap" ;
		}
		if ( ! MobileUtil.isMoblie ( request ) )
		{
			return "web" ;
		}
		if ( MobileUtil.isMoblie ( request ) )
		{
			return "wap" ;
		}

		return "web" ;

	}

}
